package guiscreens;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import dao.ExecuteQuery;

/**
 * This panel handles the searching for items screen.
 * 
 * @author Lee
 * @version 1.0
 */
public class SearchItem extends JPanel {

	private CardLayout basePanel;
	private CardLayout c1;
	private JFrame thisFrame;
	private JComboBox catBox;
	private JTextField keywordField;
	private JTextField minPriceField;
	private JTextField maxPriceField;
	private JComboBox conBox;
	private String[] catValues = {"", "Art", "Books", "Electronics", "Home And Garden", "Sporting Goods", "Toys", "Other" };
	public static String keyword = "";
	public static String category = "";
	public static String minimumPrice = "";
	public static String maximumPrice = "";
	public static String condition = "";
	private String[] conValues = {"", "New", "Very Good", "Good", "Fair", "Poor" };;


	/**
	 * Create the panel.
	 */
	public SearchItem(final CardLayout c1, final JPanel basePanel, final JFrame thisFrame) {
		this.c1 = c1;
		this.basePanel = c1;
		this.thisFrame = thisFrame;
		thisFrame.setTitle("Item Search");
		thisFrame.setBounds(MainWindow.XCENTER - 200, MainWindow.YCENTER - 175, 400, 350);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 1.0, 1.0, 1.0, 1.0, 1.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		setBackground(Color.WHITE);

		JPanel panel = new JPanel();
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.anchor = GridBagConstraints.WEST;
		gbc_panel.insets = new Insets(0, 0, 5, 0);
		gbc_panel.fill = GridBagConstraints.VERTICAL;
		gbc_panel.gridx = 1;
		gbc_panel.gridy = 0;
		add(panel, gbc_panel);

		JLabel lblKeyword = new JLabel("Keyword");
		panel.add(lblKeyword);
		panel.setBackground(Color.WHITE);

		keywordField = new JTextField();
		panel.add(keywordField);
		keywordField.setColumns(10);

		JPanel panel_1 = new JPanel();
		GridBagConstraints gbc_panel_1 = new GridBagConstraints();
		gbc_panel_1.anchor = GridBagConstraints.WEST;
		gbc_panel_1.insets = new Insets(0, 0, 5, 0);
		gbc_panel_1.fill = GridBagConstraints.VERTICAL;
		gbc_panel_1.gridx = 1;
		gbc_panel_1.gridy = 1;
		add(panel_1, gbc_panel_1);

		JLabel lblCategory = new JLabel("Category");
		panel_1.add(lblCategory);

		catBox = new JComboBox(catValues);
		panel_1.add(catBox);
		panel_1.setBackground(Color.WHITE);

		JPanel panel_2 = new JPanel();
		GridBagConstraints gbc_panel_2 = new GridBagConstraints();
		gbc_panel_2.anchor = GridBagConstraints.WEST;
		gbc_panel_2.insets = new Insets(0, 0, 5, 0);
		gbc_panel_2.fill = GridBagConstraints.VERTICAL;
		gbc_panel_2.gridx = 1;
		gbc_panel_2.gridy = 2;
		add(panel_2, gbc_panel_2);

		JLabel lblMinimumPrice = new JLabel("Minimum Price $");
		panel_2.add(lblMinimumPrice);
		panel_2.setBackground(Color.WHITE);

		minPriceField = new JTextField();
		panel_2.add(minPriceField);
		minPriceField.setColumns(10);

		JPanel panel_3 = new JPanel();
		GridBagConstraints gbc_panel_3 = new GridBagConstraints();
		gbc_panel_3.anchor = GridBagConstraints.WEST;
		gbc_panel_3.insets = new Insets(0, 0, 5, 0);
		gbc_panel_3.fill = GridBagConstraints.VERTICAL;
		gbc_panel_3.gridx = 1;
		gbc_panel_3.gridy = 3;
		add(panel_3, gbc_panel_3);

		JLabel lblMaximumPrice = new JLabel("Maximum Price $");
		panel_3.add(lblMaximumPrice);

		maxPriceField = new JTextField();
		panel_3.add(maxPriceField);
		panel_3.setBackground(Color.WHITE);
		maxPriceField.setColumns(10);

		JPanel panel_4 = new JPanel();
		panel_4.setBackground(Color.WHITE);
		GridBagConstraints gbc_panel_4 = new GridBagConstraints();
		gbc_panel_4.anchor = GridBagConstraints.WEST;
		gbc_panel_4.insets = new Insets(0, 0, 5, 0);
		gbc_panel_4.fill = GridBagConstraints.VERTICAL;
		gbc_panel_4.gridx = 1;
		gbc_panel_4.gridy = 4;
		add(panel_4, gbc_panel_4);

		JLabel lblConditionAtLeast = new JLabel("Condition at least");
		panel_4.add(lblConditionAtLeast);

		conBox = new JComboBox(conValues);
		panel_4.add(conBox);

		JPanel panel_5 = new JPanel();
		GridBagConstraints gbc_panel_5 = new GridBagConstraints();
		gbc_panel_5.anchor = GridBagConstraints.EAST;
		gbc_panel_5.fill = GridBagConstraints.VERTICAL;
		gbc_panel_5.gridx = 1;
		gbc_panel_5.gridy = 5;
		add(panel_5, gbc_panel_5);

		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				thisFrame.setBounds(MainWindow.XCENTER - 275, MainWindow.YCENTER - 300, 550, 600);
				c1.first(basePanel);
			}
		});
		panel_5.add(btnCancel);

		JButton btnSearch = new JButton("Search");
		btnSearch.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				if (!(keywordField.getText().equals(""))) {
					keyword = keywordField.getText();
					category = catValues[catBox.getSelectedIndex()];
					try { 
						Double dummy8 = Double.parseDouble(minPriceField.getText());
						Double dummy9 = Double.parseDouble(maxPriceField.getText());
						if (dummy8 > dummy9) 
							{
								JOptionPane.showMessageDialog(null, "Your min price is greater than your maximum price!");
								return;
							}
					} catch(NumberFormatException e) {}
					try {
						if (!minPriceField.getText().equals(""))
						{
							Double dummy2 = Double.parseDouble(minPriceField.getText());
							minimumPrice = minPriceField.getText();
						}
						else
							minimumPrice = 0.0 + "";
					}
					catch(NumberFormatException e) {
						minimumPrice = 0.0 + "";
						JOptionPane.showMessageDialog(null, "Wrong format, so setting minimum price to 0");
					}
					try {
						if (!maxPriceField.getText().equals(""))
						{
							Double dummy = Double.parseDouble(maxPriceField.getText());
							maximumPrice = maxPriceField.getText();
						}
						else
							maximumPrice = "99999999.99";
					} catch(NumberFormatException e) {
						maximumPrice = "99999999.99";
						JOptionPane.showMessageDialog(null, "Wrong format, so setting maximum price to a very large number");
					}
					String conditionRAW = conValues[conBox.getSelectedIndex()];
					if (conditionRAW.equals("Poor")) condition = "0";
					if (conditionRAW.equals("Fair")) condition = "1";
					if (conditionRAW.equals("Good")) condition = "2";
					if (conditionRAW.equals("Very Good")) condition = "3";
					if (conditionRAW.equals("New")) condition = "4";

					ResultSet results = ExecuteQuery.searchItem(keyword, category, minimumPrice, maximumPrice, condition);

					SearchResults search = new SearchResults(c1, basePanel, thisFrame, results);
					basePanel.add(search, "SEARCHPANEL");
					c1.show(basePanel, "SEARCHPANEL");
				}
				else JOptionPane.showMessageDialog(null, "Please enter a keyword!");
			}
		});
		panel_5.setBackground(Color.WHITE);

		panel_5.add(btnSearch);


	}

}
